#ifndef SLIPPYMAP_H
#define SLIPPYMAP_H

#include <QtCore>
#include <QtGui>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkDiskCache>

#include <math.h>

#include <iostream>
using namespace std;

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

// tile size in pixels
const int tdim = 256;

class SlippyMap: public QObject
{
    Q_OBJECT

public:
    int width;
    int height;
    int zoom;
    qreal latitude;
    qreal longitude;

    SlippyMap(QObject *parent = 0);

    void invalidate();

    void render(QPainter *p, const QRect &rect);

    void pan(const QPoint &delta);

private slots:

    void handleNetworkData(QNetworkReply *reply);

    void download();

signals:
    void updated(const QRect &rect);

protected:
    QRect tileRect(const QPoint &tp);

private:
    QPoint m_offset;
    QRect m_tilesRect;
    QPixmap m_emptyTile;
    QHash<QPoint, QPixmap> m_tilePixmaps;
    QNetworkAccessManager m_manager;
    QUrl m_url;
};


#endif // SLIPPYMAP_H
